package com.aisino.api.model;

import lombok.Data;

import java.util.List;
@Data
public class DrugInRequest {

    private String appId;
    private String timestamp;
    private String reqId;
    private String uscc;
    private String data;
    private List<DrugInData> itemList;
    private String sign;


    /**
     * 验证请求参数是否合法
     * @return 验证结果，null表示验证通过，否则返回错误信息
     */
    public ValidationResult validate() {
        if (uscc == null || uscc.trim().isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "统一社会信用代码不能为空");
        }

        if (itemList == null || itemList.isEmpty()) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "数据体不能为空");
        }

        if (itemList.size() > 1000) {
            return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "单次上传数据条数不能超过1000条");
        }

        int i=1;
        for (DrugInData item : itemList) {
            ValidationResult itemValidationResult = item.validate(i);
            i++;
            if (itemValidationResult != null) {
                return itemValidationResult;
            }
        }

        return null; // 验证通过
    }
    @Data
    public static class DrugInData {
        private String ypwybsm;
        private String yppzwh;
        private Integer crklx;
        private String crksj;
        private String crkck;
        private String jbr;
        private String scrq;
        private String cpph;
        private String yxqz;
        private Double sl;
        private Double dj;
        private String gysmc;
        private String gystyxydm;
        private String ypzsm;
        private String bz;


        /**
         * 验证药品入库数据项是否合法
         * @return 验证结果，null表示验证通过，否则返回错误信息
         */
        public ValidationResult validate(int i) {
            if (ypwybsm == null || ypwybsm.trim().isEmpty()) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据药品唯一标识码不能为空");
            }
            if (yppzwh == null || yppzwh.trim().isEmpty()) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据yppzwh不能为空");
            }

            if (crklx == null || crklx < 1 || crklx > 9) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据入库类型必须为1(采购入库), 2(退货入库)");
            }

            if (crksj == null || crksj.trim().isEmpty()) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据入库时间不能为空");
            }
            
            // 验证入库时间格式是否为 yyyy-MM-dd HH:mm:ss
            if (!crksj.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据入库时间格式不正确，应为yyyy-MM-dd HH:mm:ss");
            }

//            if (jbr == null || jbr.trim().isEmpty()) {
//                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据经办人姓名不能为空");
//            }

            if (scrq == null || scrq.trim().isEmpty()) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据生产日期不能为空");
            }
            
            // 验证生产日期格式是否为 yyyy-MM-dd
            if (!scrq.matches("\\d{4}-\\d{2}-\\d{2}")) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据生产日期格式不正确，应为yyyy-MM-dd");
            }

            if (cpph == null || cpph.trim().isEmpty()) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据产品批号不能为空");
            }
            if(cpph.length()>30){
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据产品批号长度不能超过30");
            }
            if (yxqz == null || yxqz.trim().isEmpty()) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据有效期至不能为空");
            }
            
            // 验证有效期至格式是否为 yyyy-MM-dd
            if (!yxqz.matches("\\d{4}-\\d{2}-\\d{2}")) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据有效期至格式不正确，应为yyyy-MM-dd");
            }

            if (sl == null || sl <= 0) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据药品数量必须大于0");
            }
            
            // 验证药品数量不能大于千万(10,000,000)
            if (sl > 10000000) {
                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据药品数量不能大于千万");
            }

            if (dj == null) {
                dj = 0.00;
            }

//            if (gysmc == null || gysmc.trim().isEmpty()) {
//                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据供应商名称不能为空");
//            }
//
//            if (gystyxydm == null || gystyxydm.trim().isEmpty()) {
//                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "第"+i+"条数据供应商统一信用代码不能为空");
//            }

//            if (ypzsm == null || ypzsm.trim().isEmpty()) {
//                return new ValidationResult(ResultCode.PARAM_ERROR.getCode(), "药品追溯码不能为空");
//            }

            return null; // 验证通过
        }
    }

    /**
     * 验证结果类
     */
    public static class ValidationResult {
        private int code;
        private String message;

        public ValidationResult(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }
    }
}
